package com.check.controller;

import com.check.bean.DataList;
import com.check.bean.LoggerString;
import com.check.bean.UiList;
import com.check.ui.StageModal;
import com.check.utils.LogUtils;
import com.check.utils.PropertiesUtils;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

import java.net.URL;
import java.util.*;

/**
 * @description:
 * @Author: zyt
 */
public class CheckController implements Initializable {
    @FXML
    private VBox headList;
    @FXML
    private HBox checkList;
    @FXML
    private AnchorPane pane;

    @FXML
    private TextField text;

    // 上次搜索出来的节点
    private Node node;
    // 上次搜索出来节点的数据
    private String nodeStyle;


    private DataList dataList = DataList.getDataList();

    private UiList uiList = UiList.getUIList();
    private List<String> checks = new ArrayList<>();
    private List<String> checks1 = new ArrayList<>();
    private List<String> checks2 = new ArrayList<>();
    private List<String> checks3 = new ArrayList<>();
    private List<String> checks4 = new ArrayList<>();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // 构建默认的检查项

        checks1.add("唯一性");
        checks1.add("必填项");

        checks2.add("邮箱");
        checks2.add("手机号");
        checks2.add("身份证");
        checks2.add("手机加座机");
        checks2.add("日期");
        checks2.add("日期加时间");

        checks3.add("小数");
        checks3.add("整数");
        checks3.add("数字加字母");
        checks3.add("非中文");
        checks3.add("字符");

        Properties properties = PropertiesUtils.getPropertiesValue("/checkRule.properties");
        Set<String> objects = properties.stringPropertyNames();
        checks4.addAll(objects);
        checks.addAll(checks1);
        checks.addAll(checks2);
        checks.addAll(checks3);
        checks.addAll(checks4);
        dataList.setChecks(checks);
        // List<String> list = new ArrayList<>();
        // list.add("藏三");
        // list.add("lisi1");
        // list.add("lisi2");
        // list.add("lisi3");
        // list.add("lisi4");
        // list.add("lisi5");
        // list.add("lisi6");
        // list.add("lisi7");
        // list.add("ww");
        // dataList.setHeads(list);
        initUi();
    }

    @FXML
    public void search(){
        if (node!=null){
            node.setStyle(nodeStyle);
        }
        String searchText = text.getText();
        if (searchText.length()==0){
            return;
        }
        pane.lookupAll("#"+searchText).forEach(k->{
            node = k.getParent();
            nodeStyle = node.getStyle();
            node.setStyle("-fx-background-color: crimson");
        });
    }

    private void initUi(){
        // 从数据列表中获取要检查的excel表格的标题
        List<String> heads = dataList.getHeads();

        Border border = new Border((new BorderStroke(Paint.valueOf("#000000"), BorderStrokeStyle.SOLID, new CornerRadii(0), new BorderWidths(3))));
        headList.setMinWidth(80);
        headList.setBorder(border);
        // 初始化检查
        VBox vBox1 = new VBox();
        VBox vBox2 = new VBox();
        VBox vBox3 = new VBox();
        VBox vBox4 = new VBox();
        // 初始化表格头
        initCheckListVBox(vBox1,vBox2,vBox3,vBox4);
        // 设置表格头
        setHead(vBox1,"      基本规则");
        setHead(vBox2,"      特殊规则");
        setHead(vBox3,"   数据类型规则");
        setHead(vBox4,"      新添规则");
        // 设置边框为下边框显示 边框长度3 组件距离边框无间隔
        Border foot = new Border(new BorderStroke(
                Paint.valueOf("#000000"),Paint.valueOf("#000000"),Paint.valueOf("#000000"),Paint.valueOf("#000000"),
                BorderStrokeStyle.NONE, BorderStrokeStyle.NONE, BorderStrokeStyle.SOLID, BorderStrokeStyle.NONE,
                new CornerRadii(0), new BorderWidths(3),Insets.EMPTY));
        int col = 0;
        // 设置背景颜色
        final Background background = new Background(new BackgroundFill(Color.color(0.8,0.8,0.8), CornerRadii.EMPTY, Insets.EMPTY));
        // 初始化检查头
        this.addHead(headList,"",1,1,background,foot);

        if(heads != null && heads.size() > 0){
            // 为每一个列构建选择项
            for (String head : heads) {

                if(head.equals("序号")){
                    continue;
                }

                int length = max(checks1.size(),checks2.size(),checks3.size(),checks4.size());
                col++;
                // 构建表格头
                this.addHead(headList,head,length,col,background,foot);
                // 获取最大长度
                // 构建相对应的多选框容器
                this.addCheckBox(vBox1,head,checks1,col,background,length,foot);
                this.addCheckBox(vBox2,head,checks2,col,background,length,foot);
                this.addCheckBox(vBox3,head,checks3,col,background,length,foot);
                this.addCheckBox(vBox4,head,checks4,col,background,length,foot);
            }
        }
    }

    @FXML
    public void hideStage(){
        uiList.getCheck().getStage().hide();
    }

    @FXML
    // 获取每个选择框的选中情况
    public void getSelect(){
        // 从数据列表中获取要检查的excel表格的标题
        List<String> heads = dataList.getHeads();
        // 初始化多选框选中情况的数据模型
        Map<String,List<String>> checkMap = new HashMap<>();
        for (String head : heads) {

            if (head.equals("序号")){
                continue;
            }
            List<String> list = new ArrayList<>();
            for (String check : checks) {
                // 根据checkBox 的id获取选中框
                if(head.equals("错误原因!")){
                    break;
                }
                boolean b  = ((CheckBox)(pane.lookup("#" + head + "-" + check))).isSelected();
                if(b){
                    list.add(check);
                }
            }
            if(list.size()>0){
                checkMap.put(head,list);
            }
        }
        checkMap.forEach((k,v)->{
            LoggerString.stringBuffer.append("检查字段:"+k+",检查规则:"+v+";\n");
        });

        // 将数据存放到数据列表中
        dataList.setCheckMap(checkMap);
        uiList.getCheck().getStage().hide();
        Stage stage = new StageModal("正在检查数据!请稍后!");
        LogUtils.appendLog("检查以加载的表格:检查项详细为:"+checkMap);

        uiList.getOneTableCheckController().tableViewInit2();
        stage.close();
    }

    /**
     * 求最大值
     * @param arry
     * @return
     */
    private int max(int ... arry) {
        Arrays.sort(arry);
        return  arry[arry.length-1];
    }

    /**
     * 初始化 检查项的vBox
     * @param vBoxs
     */
    private void initCheckListVBox(VBox ... vBoxs){
        Border border = new Border(new BorderStroke(
                Paint.valueOf("#000000"),Paint.valueOf("#000000"),Paint.valueOf("#000000"),Paint.valueOf("#000000"),
                BorderStrokeStyle.SOLID, BorderStrokeStyle.SOLID, BorderStrokeStyle.SOLID, BorderStrokeStyle.SOLID,
                new CornerRadii(0), new BorderWidths(3,3,3,0),Insets.EMPTY));
        for (VBox vBox : vBoxs) {
            vBox.setBorder(border);
            vBox.setPrefHeight(400);
        }
        checkList.getChildren().setAll(vBoxs);
    }

    /**
     * 初始化检查头表格
     * @param vBox
     * @param headStr
     */
    private void setHead(VBox vBox,String headStr){
        // 边框
        Border border = new Border(new BorderStroke(
                Paint.valueOf("#000000"),Paint.valueOf("#000000"),Paint.valueOf("#000000"),Paint.valueOf("#000000"),
                BorderStrokeStyle.NONE, BorderStrokeStyle.NONE, BorderStrokeStyle.SOLID, BorderStrokeStyle.NONE,
                new CornerRadii(0), new BorderWidths(3),Insets.EMPTY));
        HBox hBox = new HBox();
        hBox.setMinHeight(31);
        hBox.setBorder(border);
        Label label1 = new Label(headStr);
        hBox.getChildren().add(label1);
        vBox.getChildren().add(hBox);
    }

    /**
     * 初始化检查头对应的每个检查项
     * @param vBox1
     * @param head
     * @param checks
     * @param col
     * @param background
     * @param length
     * @param border
     */
    private void addCheckBox(VBox vBox1,String head,List<String> checks,int col,Background background,int length,Border border){
        VBox vBox = new VBox();
        if (col%2==0){
            vBox.setBackground(background);
        }
        // 构建每个列对应的多选框
        for (String check : checks) {
            CheckBox checkBox = new CheckBox(check);
            // 为每个多选框生成相对应的id
            checkBox.setId(head+"-"+check);
            checkBox.setMinHeight(25);
            vBox.getChildren().add(checkBox);
        }
        for (int i = 0; i < length-checks.size(); i++) {
            Label label1 = new Label();
            label1.setMinHeight(25);
            vBox.getChildren().add(label1);
        }

        vBox.setBorder(border);
        vBox1.getChildren().add(vBox);

    }

    /**
     * 构建检查项
     * @param headList
     * @param head
     * @param length
     * @param col
     * @param background
     * @param border
     */
    private void addHead(VBox headList, String head, int length, int col,Background background,Border border){
        VBox vBox = new VBox();
        // 构建每个标题的头
        Label label = new Label(head);
        label.setId(head);
        label.setMinHeight(25);
        vBox.getChildren().addAll(label);
        // 根据最大长度添加head的高度
        if (col%2==0){
            vBox.setBackground(background);
        }
        for (int i = 0; i < length-1; i++) {
            label = new Label();
            label.setMinHeight(25);
            vBox.getChildren().addAll(label);
        }
        vBox.setBorder(border);
        headList.getChildren().add(vBox);
    }
}
